﻿function REmail(tool){
    if(!tool){return;}
	this.tool=tool; 
	this.isLock=true;
	this.maplet=null;
	
	this.eReceiver="收件人邮箱";
	this.eTitle="请填写邮件的标题";
	this.eContent="请填写邮件的内容，本站将会把内容与当前地图的网址一并发送到收件人邮箱";
	
	this.div=new RDiv(0,0,null,null).get();
    this.div.id="REmail";
	this.div.style.display="none";
	this.div.style.visibility="hidden";
    document.body.appendChild(this.div);
	this.innerdiv=new RDiv(0,0,null,null).get();
	this.div.appendChild(this.innerdiv);
	this.style=this.div.style;
	
    if(!this.closebtn){
	    this.closebtn=new RImage(this.width-30,13,'/img/favoriteClose.gif').get();
    }
    this.closebtn.style.cursor='pointer';
    this.closebtn.style.left='470px';
    this.closebtn.style.top='8px';
    this.closebtn.root=this;
    this.div.appendChild(this.closebtn);
    this.closebtn.onclick=function(){
        this.root.turnoff();
        this.root.innerdiv.innerHTML='';
//        this.root.setInnerHTML();
    }

	this.setInnerHTML=function(){
	    this.innerdiv.innerHTML = '<table style="width:500px; background-color:#FFFFFF; border:1px solid #44A7D1;">'
	        +'<tr><td colspan="3" style="width:100%; height:30px; background-image:url(/img/FavoriteTitlebg.gif);"><table>'
                +'<tr><td style="width:5px;"></td><td  style="color: #ffffff;font-size: 14px; font-weight:bold;">发送邮件</td></tr>'
            +'</table></td></tr>'
            +'<tr><td style="width:10px;"></td><td>收件人：</td><td><input id="emailReceiver" name="" type="text" size="50" style="color:#CCCCCC;" value="收件人邮箱" /></td></tr>'
            +'<tr><td style="width:10px;"></td><td>标　题：</td><td><input id="emailTitle" name="" type="text" size="50" style="color:#CCCCCC;" value="请填写邮件的标题"/></td></tr>'
            +'<tr><td style="width:10px;"></td><td style="vertical-align:top;">内　容：</td><td><textarea id="emailContent" name="" cols="45" rows="8" style="color:#CCCCCC;" ></textarea></td></tr>'
            +'<tr><td style="width:10px;"></td><td></td><td><input id="sEmail" type="button" name="" value="发 送" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
            +'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input id="exitEmail" type="button" name="" value="取 消" /></td></tr>'
            +'<tr><td colspan="3" style="height:10px"><iframe id="emailIframe" style="display:none;" src="sandEmail.aspx"></iframe></td></tr>'
            +'</table>';
    document.frames['emailIframe'].location.reload();//刷新iframe
        
        if(document.getElementById('sEmail')){
            document.getElementById('sEmail').root=this;
            document.getElementById('sEmail').onclick=function(){
                this.root.sendEmail();
            }
        }
        if(document.getElementById('exitEmail')){
            document.getElementById('exitEmail').root=this;
            document.getElementById('exitEmail').onclick=function(){
                this.root.turnoff();
//                this.root.setInnerHTML();
                this.root.innerdiv.innerHTML='';
            }
        }
        
        if(document.getElementById('emailReceiver')){
            document.getElementById('emailReceiver').root=this;
            document.getElementById('emailReceiver').onfocus=function(){
                this.style.color = "#000000";
                if(trim(this.value)==this.root.eReceiver){
                    this.value="";
                }
            }
            document.getElementById('emailReceiver').onblur=function(){
                if(trim(this.value)==""){
                    this.style.color = "#CCCCCC";
                    this.value=this.root.eReceiver;
                }
            }
        }
        if(document.getElementById('emailTitle')){
            document.getElementById('emailTitle').root=this;
            document.getElementById('emailTitle').onfocus=function(){
                this.style.color = "#000000";
                if(trim(this.value)==this.root.eTitle){
                    this.value="";
                }
            }
            document.getElementById('emailTitle').onblur=function(){
                if(trim(this.value)==""){                
                    this.style.color = "#CCCCCC";
                    this.value=this.root.eTitle;
                }
            }
        }
        if(document.getElementById('emailContent')){
            if(document.getElementById('emailContent').value==""){
                document.getElementById('emailContent').value=this.eContent;
            }
            document.getElementById('emailContent').root=this;
            document.getElementById('emailContent').onfocus=function(){
                this.style.color = "#000000";
                if(trim(this.value)==this.root.eContent){
                    this.value="";
                }
            }
            document.getElementById('emailContent').onblur=function(){
                if(trim(this.value)==""){                
                this.style.color = "#CCCCCC";
                    this.value=this.root.eContent;
                }
            }
        }
    }

    this.sendEmail = function(){
        var emExp=/^([a-zA-Z0-9]+[_|\_|\.\-]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
        if(trim(document.getElementById('emailReceiver').value).match(emExp)!=null){
            if(trim(document.getElementById('emailTitle').value.replace(this.eTitle,''))!=""){
                var emailIframe = document.frames['emailIframe'];
                emailIframe.document.getElementById('emailReceiver').value = trim(document.getElementById('emailReceiver').value.replace(this.eReceiver,''));
                emailIframe.document.getElementById('emailTitle').value = trim(document.getElementById('emailTitle').value.replace(this.eTitle,''));
                emailIframe.document.getElementById('emailContent').value = trim(document.getElementById('emailContent').value.replace(this.eContent,''));
                emailIframe.document.getElementById('emailUrl').value = location.href;
                emailIframe.document.getElementById('btnSendEmail').click();
                this.turnoff();
//                this.setInnerHTML();emailUrl
            }else{
                alert('请填写邮件标题');
            }
        }else{
            alert('请输入正确的邮箱地址');
        }
    }
    
	this.get = function(){}
        
	if(tool.src){
	    this.imgpath=this.tool.src;
	}
	
	this.toString=function(){
		return 'REmail';
	}
	
	this.turnon=function(){	    
	    if(this.isLock){	               
	        return;
	    }
	    if(this.innerdiv.innerHTML==""){
	        this.setInnerHTML();
        }
        
	    this.div.style.left=getBoundingClientRect(this.tool).left-470;
        this.div.style.top=getBoundingClientRect(this.tool).top+25;
        this.div.style.display="block";
        this.div.style.visibility = 'visible';
        
	    this.notify('onturnon');
    }
    
    this.turnoff=function(){
	    this.isLock=true;
	    this.div.style.display="none";
        this.div.style.visibility = 'hidden';
		if(this.imgpath){
		    this.tool.src=this.imgpath;
		}
		this.notify('onturnoff');
    }
    
    this.clear=function(){};
	
	this.setMaplet=function(maplet){ 
		this.maplet=maplet;		
	}
	
	this.mClick=function(evt){
        if(this.div.style.display!="none"){
            var element=null;
            if(window.event){
                element=window.event.srcElement;
            }else{
                element=evt.originalTarget;
            }
            while(element.tagName!='BODY'){
                if(element.id=='REmail'||element.id==this.tool.id){                 
                    return;
                }
                element=element.parentElement||element.parentNode;
            }
            this.turnoff();
        }
    }
	
	if(window.event){
	    g_addListener("click",this.mClick,this);
	    document.attachEvent("onclick",g_notify);
    }else{
        g_addListener("click",this.mClick,this);
        document.addEventListener("click",g_notify,false);
    }
	
	this.onbegin=new Array();
	this.onsuccess=new Array();
	this.onturnon=new Array();
	this.onturnoff=new Array();
	
	this.addListener=function(listener,estring){
		switch(estring){
			case 'onbegin':
 				if(listener.onbegin){
 					this.onbegin[this.onbegin.length]=listener;
 				} else{
 					window.status=this.toString()+':不合法的'+estring+'事件对象';
 				}
			break;
			case 'onsuccess':
 				if(listener.onsuccess){
 					this.onsuccess[this.onsuccess.length]=listener;
 				} else{
 					window.status=this.toString()+':不合法的'+estring+'事件对象';
 				}
			break;
			case 'onturnon':
 				if(listener.onturnon){
 					this.onturnon[this.onturnon.length]=listener;
 				} else{
 					window.status=this.toString()+':不合法的'+estring+'事件对象';
 				}
			break;
			case 'onturnoff':
 				if(listener.onturnoff){
 					this.onturnoff[this.onturnoff.length]=listener;
 				} else{
 					window.status=this.toString()+':不合法的'+estring+'事件对象';
 				}
			break;
			default:
				window.status=this.toString()+'类不存在'+estring+'事件';
			break;
		}
	}
	
	this.notify=function(estring){
		switch(estring){
			case 'onbegin':
 				for(var i=0;i<this.onbegin.length;i++){
 					if(this.onbegin[i].onbegin){
 						this.onbegin[i].onbegin();
 					}else{ 
 						window.status=this.toString()+'类的某'+estring+'事件监听对象损坏';
 					}
 				}
			break;
			case 'onsuccess':
 				for(var i=0;i<this.onsuccess.length;i++){
 					if(this.onsuccess[i].onsuccess){
 						this.onsuccess[i].onsuccess();
 					}else{ 
 						window.status=this.toString()+'类的某'+estring+'事件监听对象损坏';
 					}
 				}
			break;
			case 'onturnon':
 				for(var i=0;i<this.onturnon.length;i++){
 					if(this.onturnon[i].onturnon){
 						this.onturnon[i].onturnon();
 					}else{ 
 						window.status=this.toString()+'类的某'+estring+'事件监听对象损坏';
 					}
 				}
			break;
			case 'onturnoff':
 				for(var i=0;i<this.onturnoff.length;i++){
 					if(this.onturnoff[i].onturnoff){
 						this.onturnoff[i].onturnoff();
 					}else{ 
 						window.status=this.toString()+'类的某'+estring+'事件监听对象损坏';
 					}
 				}
			break;
			default:
				window.status=this.toString()+'类不存在'+estring+'事件';
			break;
		}
	}
	
}